home *** CD-ROM | disk | FTP | other *** search
/ Champak 141 / (Vol 141) Oct 17 2011.iso / Games / flight-of-the-museum.swf / scripts / com / google / analytics / debug / UISprite.as < prev    next >
Encoding:
Text File  |  2011-10-17  |  5.3 KB  |  205 lines

  1. package com.google.analytics.debug
  2. {
  3.    import flash.display.DisplayObject;
  4.    import flash.display.Sprite;
  5.    import flash.display.Stage;
  6.    import flash.events.Event;
  7.    
  8.    public class UISprite extends Sprite
  9.    {
  10.        
  11.       
  12.       private var _forcedWidth:uint;
  13.       
  14.       public var margin:Margin;
  15.       
  16.       protected var alignTarget:DisplayObject;
  17.       
  18.       protected var listenResize:Boolean;
  19.       
  20.       public var alignement:Align;
  21.       
  22.       private var _forcedHeight:uint;
  23.       
  24.       public function UISprite(alignTarget:DisplayObject = null)
  25.       {
  26.          super();
  27.          listenResize = false;
  28.          alignement = Align.none;
  29.          this.alignTarget = alignTarget;
  30.          margin = new Margin();
  31.          addEventListener(Event.ADDED_TO_STAGE,_onAddedToStage);
  32.          addEventListener(Event.REMOVED_FROM_STAGE,_onRemovedFromStage);
  33.       }
  34.       
  35.       public function get forcedHeight() : uint
  36.       {
  37.          if(_forcedHeight)
  38.          {
  39.             return _forcedHeight;
  40.          }
  41.          return height;
  42.       }
  43.       
  44.       private function _onAddedToStage(event:Event) : void
  45.       {
  46.          layout();
  47.          resize();
  48.       }
  49.       
  50.       protected function dispose() : void
  51.       {
  52.          var d:DisplayObject = null;
  53.          for(var i:int = 0; i < numChildren; i++)
  54.          {
  55.             d = getChildAt(i);
  56.             if(d)
  57.             {
  58.                removeChild(d);
  59.             }
  60.          }
  61.       }
  62.       
  63.       public function set forcedHeight(value:uint) : void
  64.       {
  65.          _forcedHeight = value;
  66.       }
  67.       
  68.       public function set forcedWidth(value:uint) : void
  69.       {
  70.          _forcedWidth = value;
  71.       }
  72.       
  73.       protected function layout() : void
  74.       {
  75.       }
  76.       
  77.       public function get forcedWidth() : uint
  78.       {
  79.          if(_forcedWidth)
  80.          {
  81.             return _forcedWidth;
  82.          }
  83.          return width;
  84.       }
  85.       
  86.       public function alignTo(alignement:Align, target:DisplayObject = null) : void
  87.       {
  88.          var H:uint = 0;
  89.          var W:uint = 0;
  90.          var X:uint = 0;
  91.          var Y:uint = 0;
  92.          var t:UISprite = null;
  93.          if(target == null)
  94.          {
  95.             if(parent is Stage)
  96.             {
  97.                target = this.stage;
  98.             }
  99.             else
  100.             {
  101.                target = parent;
  102.             }
  103.          }
  104.          if(target == this.stage)
  105.          {
  106.             if(this.stage == null)
  107.             {
  108.                return;
  109.             }
  110.             H = uint(this.stage.stageHeight);
  111.             W = uint(this.stage.stageWidth);
  112.             X = 0;
  113.             Y = 0;
  114.          }
  115.          else
  116.          {
  117.             t = target as UISprite;
  118.             if(t.forcedHeight)
  119.             {
  120.                H = t.forcedHeight;
  121.             }
  122.             else
  123.             {
  124.                H = t.height;
  125.             }
  126.             if(t.forcedWidth)
  127.             {
  128.                W = t.forcedWidth;
  129.             }
  130.             else
  131.             {
  132.                W = t.width;
  133.             }
  134.             X = 0;
  135.             Y = 0;
  136.          }
  137.          switch(alignement)
  138.          {
  139.             case Align.top:
  140.                x = W / 2 - forcedWidth / 2;
  141.                y = Y + margin.top;
  142.                break;
  143.             case Align.bottom:
  144.                x = W / 2 - forcedWidth / 2;
  145.                y = Y + H - forcedHeight - margin.bottom;
  146.                break;
  147.             case Align.left:
  148.                x = X + margin.left;
  149.                y = H / 2 - forcedHeight / 2;
  150.                break;
  151.             case Align.right:
  152.                x = X + W - forcedWidth - margin.right;
  153.                y = H / 2 - forcedHeight / 2;
  154.                break;
  155.             case Align.center:
  156.                x = W / 2 - forcedWidth / 2;
  157.                y = H / 2 - forcedHeight / 2;
  158.                break;
  159.             case Align.topLeft:
  160.                x = X + margin.left;
  161.                y = Y + margin.top;
  162.                break;
  163.             case Align.topRight:
  164.                x = X + W - forcedWidth - margin.right;
  165.                y = Y + margin.top;
  166.                break;
  167.             case Align.bottomLeft:
  168.                x = X + margin.left;
  169.                y = Y + H - forcedHeight - margin.bottom;
  170.                break;
  171.             case Align.bottomRight:
  172.                x = X + W - forcedWidth - margin.right;
  173.                y = Y + H - forcedHeight - margin.bottom;
  174.          }
  175.          if(!listenResize && alignement != Align.none)
  176.          {
  177.             target.addEventListener(Event.RESIZE,onResize,false,0,true);
  178.             listenResize = true;
  179.          }
  180.          this.alignement = alignement;
  181.          this.alignTarget = target;
  182.       }
  183.       
  184.       private function _onRemovedFromStage(event:Event) : void
  185.       {
  186.          removeEventListener(Event.ADDED_TO_STAGE,_onAddedToStage);
  187.          removeEventListener(Event.REMOVED_FROM_STAGE,_onRemovedFromStage);
  188.          dispose();
  189.       }
  190.       
  191.       public function resize() : void
  192.       {
  193.          if(alignement != Align.none)
  194.          {
  195.             alignTo(alignement,alignTarget);
  196.          }
  197.       }
  198.       
  199.       protected function onResize(event:Event) : void
  200.       {
  201.          resize();
  202.       }
  203.    }
  204. }
  205.